﻿// 代理客户端formProcess,格式化输出
export enum SendMode {
  HTTP,
  SMTP,
  MOBILE,
}

declare global {
  interface NationData {
    location_code: number
    location_name: string
    location_code_parent: string
    counTry_iso_code: string
    location_type: string
    geo_name: string
    geo_id: string
    geo_name_CN: string
    ObjectDBID: number
  }

  interface DomainData {
    domain: string
    Path: string
    url: string
    desc: string
    title: string
    url_shown: string
    ObjectDBID: number
  }

  export interface EmailData {
    email: string
    verified: boolean
    status: string
    ObjectDBID: number
  }

  interface MailType {
    Name: string
    Smtp: string
    SSL: boolean
    Port: number
  }

  interface SenderInfo {
    Account: string
    Password: string
    MailType: MailType
    SuccessCount?: number
    FailCount?: number
    AllCount?: number
  }

  interface ReceiverInfo {
    Name: string
    MailNick: string
    Mail: string
    SendCount?: number
    SendTime?: Date
  }

  interface Content {
    Title: string
    Content: string
    PicEncrypt?: boolean
    ContentEncrypt?: boolean
    FilePath?: string
  }

  interface ReportSendEmail {
    SenderEmail: string
    SuccessCount: string
    FailCount: string
    SendMode: string
  }
  enum ExpandKeywordsChannel {
    Google,
    Yahoo,
    Baidu,
  }

  // interface FormProcess {
  //   // 获取国家列表
  //   DataSearchNation: () => string
  //   // 根据国家、关键词查询域名信息
  //   DataSearchDomain: (nation: string[], keywords: string) => string
  //   // 获取域名下的邮箱信息
  //   DataSearchEmail: (domain: string[]) => string
  //   // 扩展关键词查询
  //   ExpandKeywords: (keyword: string, channel: ExpandKeywordsChannel) => string
  // }

  export interface FormProcessProxy {
    // 获取国家列表
    DataSearchNation: () => NationData[]
    // 根据国家、关键词查询域名信息
    DataSearchDomain: (
      nation: string[],
      keywords: string
    ) => { [key: string]: DomainData[] } | any
    // 获取域名下的邮箱信息
    DataSearchEmail: (domain: string[]) => { [key: string]: EmailData[] } | any
    // 扩展关键词查询
    ExpandKeywords: (keyword: string, channel: ExpandKeywordsChannel) => string
    SetCloseButtonVisible: (visible: boolean) => string
    Exit: () => string
    MinSize: () => string
    Drag: () => string
    ChangeSize: () => string
    /**
     * 获取当前运行中的邮件发送营销任务id数组
     */
    RunningTasks: () => number[]
    /**
     * 发送邮件
     * @param Sender 发件人Json数据
     * @param Receiver 收件人Json数据
     * @param Content 发件内容Json数据
     * @param ContentSettings 发件内容配置数据
     * @param MinInterval 最小间隔时间
     * @param MaxInterval 最大间隔时间
     * @param RandName 昵称
     * @param sendMode 发送模式：HTTP=0, SMTP=1, MOBILE=2
     * @param SendCount 单个发件箱最大发送数量限制
     * @param AllCount 总发送数量限制
     * @param OnceTimeMulti 是否设置单次邮件发送数量限制
     * @param OnceTimeNum 单次邮件发送数量限制
     * @param ReplyTo
     * @param OpenADSL 是否设置全局ADSL拨号设置
     * @param ADSL_Count ADSL连续发送数量限制
     * @param ADSL_EN 宽带名称
     * @param ADSL_U 用户名
     * @param ADSL_P 密码
     * @param OpenSIM7600 是否启用全局设备换IP
     * @param MarketingTaskID 营销任务ID
     * @param MarketingTaskName 营销任务名称
     * @param sendMethod 0 随机发送，1 顺序发送
     * @param sendTemplateMethod 0 随机发送，1 顺序发送
     */
    SendEmail(
      Sender: string | SenderInfo,
      Receiver: string | ReceiverInfo,
      Content: string | Content,
      ContentSettings: string,
      MinInterval: number,
      MaxInterval: number,
      RandName: string,
      sendMode: SendMode,
      SendCount: number,
      AllCount: number,
      OnceTimeMulti: boolean,
      OnceTimeNum: number,
      ReplyTo: string,
      OpenADSL: boolean,
      ADSL_Count: number,
      ADSL_EN: string,
      ADSL_U: string,
      ADSL_P: string,
      OpenSIM7600: boolean,
      MarketingTaskID: number,
      MarketingTaskName: string,
      sendMethod: number,
      sendTemplateMethod: number
    ): string

    // 第三方授权
    Auth(url: string): string

    VerifyEmail: (email: string) => boolean

    VerifyEmails: (emails: []) => []
    PauseSendEmail: () => void
    ResumeSendEmail: () => void
    AbortSendEmail: () => void
    IsPaused: () => boolean

    VerifySmtp: (
      host: string,
      port: number,
      useSsl: boolean,
      username: string,
      password: string
    ) => string

    ShowDebugButton(): void

    GetLogo(): string
    /**
     * 获取地图商家信息
     * @param address
     * @param keywords
     */
    GetMerchants(address: string, keywords: string): string

    /**
     * 获取地图商家信息
     * @param json
     * @param fileType
     */
    SaveFile(json: string, fileType: string): string
  }
}

// @ts-ignore
const formProxy: FormProcessProxy = new Proxy(window?.formProcess || {}, {
  get(target: any, p: string | symbol, receiver: any): any {
    // @ts-ignore
    const method = target[p] || undefined
    if (typeof method !== 'function') return method

    return function () {
      // eslint-disable-next-line prefer-rest-params
      console.log(`调用客户端${p.toString()},参数: `, ...arguments)
      // eslint-disable-next-line prefer-rest-params
      const res = method(...arguments)
      console.log(`${p.toString()}返回(raw): `, res)
      if (!res) return res
      const json: { Data: any; state: any } = JSON.parse(res)
      console.log(`${p.toString()}返回(jobj): `, json)
      if (json.state === true) {
        let jRes
        try {
          jRes = JSON.parse(json.Data)
        } catch {
          return json.Data
        }
        return jRes
      }
      console.warn('返回格式不是{data,state},', json)
      // throw new Error(json.Data)
      return json
    }
  },
})
export default formProxy
